package com.cskaoyan.salary;

import com.cskaoyan.salary.bean.Employee;
import com.cskaoyan.salary.bean.Record;
import com.cskaoyan.salary.utils.DruidUtils;
import com.cskaoyan.salary.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/record")
public class RecordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //
        DataSource dataSource = DruidUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        List<Record> records = null;
        try {
            records = queryRunner.query("select t1.name as `in` ,t2.id,t2.money,t2.transfer,t3.name as `out` from " +
                    "t_employee t1, t_record t2, t_employee t3 where t1.id = t2.in and t2.out = t3.id;", new BeanListHandler<>(Record.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        finally {
            try {
                DruidUtils.getConnection().close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        request.setAttribute("records", records);
        request.getRequestDispatcher("/records_bak.jsp").forward(request, response);

    }
}
